package com.hzit.loan.pay.service;

import com.hzit.loan.pay.contstant.RedisKeyConstant;
import com.hzit.loan.pay.mapper.MchInfoMapper;
import com.hzit.loan.pay.model.MchInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.concurrent.TimeUnit;

/**
 * 获取商户信息
 */
@Service
public class McInfoServiceImpl implements IMcInfoService{

    @Autowired
    private MchInfoMapper mchInfoMapper;

    @Autowired
    private ValueOperations<String,Object> valueOperations;

    @Override
    public MchInfo queryMchInfoById(String mchId) {

        //先查询redis
        String redisKey = RedisKeyConstant.MCHINFO_KEY + mchId;
        MchInfo mchInfo = (MchInfo) valueOperations.get(redisKey);
        if (ObjectUtils.isEmpty(mchInfo)){
             mchInfo =  mchInfoMapper.queryMchInfoByMchId(mchId);
            valueOperations.set(redisKey,mchInfo,2, TimeUnit.HOURS);
        }
        return mchInfo;
    }
}
